package com.zhq.apputil.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.VideoView;

import com.zhq.apputil.R;
import com.zhq.apputil.utils.ScreenUtils;

/***
 * 播放视频View
 */
public class CustomVideoView extends VideoView {
    private boolean isRound;
    private int radius;
    private Paint paint;

    public CustomVideoView(Context context) {
        super(context);
    }


    public CustomVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }


    public CustomVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);

    }

    public void init(Context mContext, AttributeSet attrs) {
        TypedArray mTypedArray = mContext.obtainStyledAttributes(attrs, R.styleable.videoView);
        isRound = mTypedArray.getBoolean(R.styleable.videoView_isRoundOverlay, false);
        radius = mTypedArray.getDimensionPixelSize(R.styleable.videoView_radius, ScreenUtils.dpToPx(mContext.getResources(), 10));
        paint = new Paint();
        paint.setDither(true);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //我们重新计算高度(这样就可以匹配不同手机的尺寸)  
        int width = getDefaultSize(0, widthMeasureSpec);
        int height = getDefaultSize(0, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isRound) {
            RectF rectF = new RectF(0.0F, 0.0F, (float) canvas.getWidth(), (float) canvas.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, paint); //先绘制圆角矩形
        }
    }
}
